use [family-annal]
GO
-- Drop trigger in table tb_fa_location
IF OBJECT_ID ('tb_fa_location.trigger_fa_location_insert_update','TR') IS NOT NULL
   DROP TRIGGER tb_fa_location.trigger_fa_location_insert_update;
GO
-- Drop table and all constraint if exists
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_location]') AND name = N'pk_tb_fa_location')
BEGIN
ALTER TABLE [dbo].[tb_fa_location] DROP CONSTRAINT [pk_tb_fa_location]
END
GO
USE [family-annal]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_location]') AND type in (N'U'))
DROP TABLE [dbo].[tb_fa_location]
GO
use [family-annal]
GO
-- create table tb_fa_location
create table tb_fa_location
(
	location_id int not null,
	person_id int,
	location_info nvarchar(max), -- use defined string json on c# source code
	create_date datetime,
	create_uid int,
	update_date datetime,
	update_uid int,
	location_status varchar(1) default 'Y'
);
GO
use [family-annal];
GO
-- create trigger trigger_fa_location_insert_update
CREATE TRIGGER trigger_fa_location_insert_update
   ON  tb_fa_location 
   AFTER INSERT, UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
    IF EXISTS(SELECT * FROM INSERTED)
    BEGIN
		update tb_fa_location set create_date = GETDATE()
		from tb_fa_location t join inserted i
		on t.location_id = i.location_id
    END
    IF NOT EXISTS(SELECT * FROM UPDATED)
    BEGIN
		update tb_fa_location set update_date = GETDATE()
		from tb_fa_location t join updated u
		on t.location_id = u.location_id;
    END
END
GO